package cn.lcl.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.system.UserInfo;
import cn.lcl.beans.Uuu;
import cn.lcl.services.UuuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UUUcontroller {
    @Resource
    private UuuService uuuService;
    Uuu user = new Uuu();

    public Uuu getuserinfo(HttpServletRequest req) {
        Uuu uu = new Uuu();
        uu.setId(Convert.toInt(req.getParameter("id")));
        uu.setUsername(req.getParameter("username"));
        uu.setPassword(req.getParameter("password"));
        return uu;
    }

    @GetMapping("/findall")
    public String findAll(ModelMap model) {
        model.addAttribute("users",uuuService.selectByExample(null));
        return "index";
    }
//    @PostMapping("/findall")
//    public String findAll1(ModelMap model) {
//        model.addAttribute("users",uuuService.selectByExample(null));
//        return "index";
//    }


    @GetMapping("/login")
    public String loginget() {
        return "login";
    }

    @PostMapping("/login")
    public String login(ModelMap model,HttpServletRequest req) {
        String username = req.getParameter("uersname");
        String userpwd = req.getParameter("userpwd");
        Uuu uuu1 = new Uuu();
        uuu1.setUsername(username);
        uuu1.setPassword(userpwd);
        Uuu login = this.uuuService.login(uuu1);
        System.out.println(login);
        req.setAttribute("msg","登录成功");
        if(login != null) {
            model.addAttribute("users",uuuService.selectByExample(null));
            req.setAttribute("msg","登录成功");
            System.out.println("1");
            return "index";
        }else {
            req.setAttribute("msg","用户名或密码错误");
            return "login";
        }
    }

    @GetMapping("/delete")
    public String deleteget(ModelMap model) {
        return "index";
    }

    @PostMapping("/delete")
    public String delete(ModelMap model,HttpServletRequest req) {
        String[] ids = req.getParameterValues("ids");
        int i = this.uuuService.deleteByIds(Convert.toIntArray(ids));
        System.out.println("i = " + i);
        if(i > 0){
            req.setAttribute("msg","删除成功");
        }else {
            req.setAttribute("msg","删除失败");
        }
        model.addAttribute("users",uuuService.selectByExample(null));
        return "index";
    }

    @GetMapping("/register")
    public String registerget(Uuu uuu) {
        return "register";
    }

    @PostMapping("/register")
    public String register(ModelMap model,HttpServletRequest req) {
        Uuu uuu2 = new Uuu();
        Uuu getuserinfo = getuserinfo(req);
        uuu2.setId(getuserinfo.getId());
        uuu2.setUsername(getuserinfo.getUsername());
        uuu2.setPassword(getuserinfo.getPassword());
        int insert = this.uuuService.insert(uuu2);
        if(insert > 0) {
            req.setAttribute("msg","注册成功");
            return "login";
        }else {
            req.setAttribute("msg","注册失败");
            return "register";
        }
    }

    @GetMapping("/update")
    public String uu() {
        return "update";
    }

    @PostMapping("/update")
    public String update(ModelMap model,HttpServletRequest req) {
        Uuu uuu3 = new Uuu();
        String id = req.getParameter("id");
        Integer idd = Integer.valueOf(id);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        System.out.println("getuserinfo.getId() = " + id);
//        System.out.println("getuserinfo.getUsername() = " + username);
//        System.out.println("getuserinfo.getPassword() = " + password);
        uuu3.setId(idd);
        uuu3.setUsername(username);
        uuu3.setPassword(password);
        int i = this.uuuService.updateByPrimaryKeySelective(uuu3);
        if(i > 0) {
            System.out.println("修改成功");
            model.addAttribute("users",uuuService.selectByExample(null));
            return "index";
        }else {
            System.out.println("修改失败");
            return "update";
        }
    }
}
